#include "Clock.h"

uint32_t clockmills = 0;

void Clock_init()
{
    uint8_t clock = CLOCK_TIMER;
    clockmills = 0;
    if(EA == 0)
    {
        EA = 1;
    }
    switch (clock)
    {
    case CLOCK_TIMER0:
        TIM0_TimeBaseInit(CLOCK_PrescalerSelection, CLOCK_MODE_TIMER);
        CLOCK_TIM0_WorkModeConfig(CLOCK_SetCounter);
        TIM0_ITConfig(ENABLE, HIGH);
        TIM0_Cmd(ENABLE);
        break;
    case CLOCK_TIMER1:
        TIM1_TimeBaseInit(CLOCK_PrescalerSelection, CLOCK_MODE_TIMER);
        CLOCK_TIM1_WorkModeConfig(CLOCK_SetCounter);
        TIM1_ITConfig(ENABLE, LOW);
        TIM1_Cmd(ENABLE);
        break;
    case CLOCK_TIMER2:
        TIM2_TimeBaseInit(CLOCK_PrescalerSelection, CLOCK_MODE_TIMER);
        CLOCK_TIM2_WorkModeConfig(CLOCK_SetCounter);
        TIM2_ITConfig(ENABLE, LOW);
        TIM2_Cmd(ENABLE);
        break;
    case CLOCK_TIMER3:
        TIM3_TimeBaseInit(CLOCK_PrescalerSelection, CLOCK_MODE_TIMER);
        CLOCK_TIM3_WorkModeConfig(CLOCK_SetCounter);
        TIM3_ITConfig(ENABLE, LOW);
        TIM3_Cmd(ENABLE);
        break;
    case CLOCK_TIMER4:
        TIM4_TimeBaseInit(CLOCK_PrescalerSelection, CLOCK_MODE_TIMER);
        CLOCK_TIM4_WorkModeConfig(CLOCK_SetCounter);
        TIM4_ITConfig(ENABLE, LOW);
        TIM4_Cmd(ENABLE);
        break;
    }
}

uint32_t Clock_mills()
{
    return clockmills;
}

#if (CLOCK_TIMER == CLOCK_TIMER0)

void timer0Interrupt(void) interrupt 1
{
    clockmills++;
    CLOCK_TIM0_ModeSetReloadCounter(CLOCK_SetCounter);
}

#endif

#if (CLOCK_TIMER == CLOCK_TIMER1)

void timer1Interrupt(void) interrupt 3
{
    clockmills++;
    CLOCK_TIM1_ModeSetReloadCounter(CLOCK_SetCounter);
}

#endif

#if (CLOCK_TIMER == CLOCK_TIMER2)

void timer2Interrupt(void) interrupt 5
{
    clockmills++;
    CLOCK_TIM2_ModeSetReloadCounter(CLOCK_SetCounter);
}

#endif

#if (CLOCK_TIMER == CLOCK_TIMER3)

void timer3Interrupt(void) interrupt 13
{
    clockmills++;
    CLOCK_TIM3_ModeSetReloadCounter(CLOCK_SetCounter);
}

#endif

#if (CLOCK_TIMER == CLOCK_TIMER4)

void timer4Interrupt(void) interrupt 14
{
    clockmills++;
    CLOCK_TIM4_ModeSetReloadCounter(CLOCK_SetCounter);
}

#endif
